﻿var moldusetimeListTableVue = new Vue({
    el:"#moldusetimeListTable",
    data: {
        isExportDisabled: false,    //用于导出时禁用按钮
        isExportLoading: false,     //用于导出时等待

        pageModel: {
            pageIdx: 1,
            pageSize: 10,
            pageTotal: 0,
            pageLength: 0,
            result: []
        },
        searchModel: {   //与SearchUseTimeViewModel实体类中相对应
            queryMoldID: "",
            querySegment: "",
            orderByID: false,
            orderByMoldID: false,
            orderBySegment: false,
            isAsc: true, 
            isAll: true
        } ,//字典 
         statusDictionary: {}        //设备状态字典
    },
    methods: {
        SelectFile: function (event) {
            var vue = this;
            event.preventDefault();

            //input只响应一次onchange，采用每次生成input[type=file]方法
            $(event.target).next("input[type=file]").remove();
            var uploadTab = '<input type="file" style="display: none" accept=".xls, .xlsx" onchange="SetFileName(this)" />';
            $(event.target).after(uploadTab);
            $(event.target).next().click();
        },
        //时间格式进行格式化
        dateFormat: function (val) {
            if (val != null) {
                var date = new Date(parseInt(val.replace("/Date(", "").replace(")/", ""), 10));
                //月份为0-11，所以+1，月份小于10时补个0
                var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
                var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
                return date.getFullYear() + "-" + month + "-" + currentDate;
            }
            return "";
        },
   
        GotoPage: function (toPageIdx) {
            this.pageModel.pageIdx = toPageIdx;
            ListMoldUseTimeForPage(this);
        },
        SearchMoldUseTime: function () {
            this.pageModel.pageIdx = 1;
            this.searchModel.isAll = false;
            ListMoldUseTimeForPage(this);
        },
        SearchReset: function () {
            ResetPageModel(this);
            ListMoldUseTimeForPage(this);
        },
        ChangeOrderByID: function(event) {
            $("a i.fa-angle-up").not(event.target).removeClass("fa-angle-down");
            $(event.target).toggleClass('fa-angle-down');
            this.searchModel.isAsc = $(event.target).hasClass('fa-angle-down') === false;
            this.searchModel.orderByID = true;
            this.searchModel.orderByMoldID = false;
            this.searchModel.orderBySegment = false;
            ListMoldUseTimeForPage(this);
        },
        ChangeOrderByMoldID: function (event) {
            $("a i.fa-angle-up").not(event.target).removeClass("fa-angle-down");
            $(event.target).toggleClass('fa-angle-down');
            this.searchModel.isAsc = $(event.target).hasClass('fa-angle-down') === false;
            this.searchModel.orderByMoldID = true;
            this.searchModel.orderBySegment = false;
            ListMoldUseTimeForPage(this);
        },
        ChangeOrderBySegment: function (event) {
            $("a i.fa-angle-up").not(event.target).removeClass("fa-angle-down");
            $(event.target).toggleClass('fa-angle-down');
            this.searchModel.isAsc = $(event.target).hasClass('fa-angle-down') === false;
            this.searchModel.orderByMoldID = false;
            this.searchModel.orderBySegment = true;
            ListMoldUseTimeForPage(this);
        },
        AddMold: function () {
            AddMoldDetail(this);  //对应下面新增模具信息  添加模具信息
        },
        EditMold: function (index) { //详细信息 --编辑信息
            EditMoldDetail(this, index);
        },
        ExportMold: function () {
            ExportMoldDetail(this); //导出信息
        },
        DeleteMold: function (index) {
            DeleteMoldDetail(this, index); //删除设备
        }
    },
    created: function () {
        //页面加载前初始的数据
        InitialAztechScript();
        //Frozencolumn();
    },
    mounted: function () {
        //页面加载后初始的数据
        this.statusDictionary = GlobalDictionary.GetMoldsStatusDictionary();

        ResetToastrOption();
        ResetPageModel(this);
        ListMoldUseTimeForPage(this);

    },
    watch: {
        "pageModel.pageSize": function (val) {
            this.pageModel.pageIdx = 1;
            ListMoldUseTimeForPage(this);
        }
    }
});



/**
 * 列出所有模具信息
 */
function ListMoldUseTimeForPage(vue) {
    vue.pageModel.results = [];
    ShowAzLoading();

    $.ajax({
        type: "POST",
        url: "/MoldUseTime/ListMoldUseTimeForPage?v=" + Math.random(),            
        data: {
            pageModel: vue.pageModel,
            searchModel: vue.searchModel
        }, 
        success: function (res) {
            if (res.status == "Success") {
                console.log(res.data);
                vue.pageModel = res.data;
            } else {
                toastr.error(res.message);
            }
            HideAzLoading();
        },
        error: function (err) {
            console.log(err);
            HideAzLoading();
            toastr.error("加载数据异常，请稍后再试或联系管理员Loading data is abnormal. Please try again later or contact the administrator。");
        }
    });
}


/**
 * 新增模具信息
 */
function AddMoldDetail(vue) {
    OpenAzModal("新增模具信息Add mold information", "/MoldUseTime/Page/MoldDetail?IsNew=true", function () {
        ListMoldUseTimeForPage(vue);
    });
    ResizeAzModal(1000);
}


/**   
 * 编辑模具信息
 */
function EditMoldDetail(vue,index) {


    //console.log(vue.data.pageModel);
    var datas = vue.$data.pageModel.results[index].BaseInfo;
    //console.log(datas.ID);

    OpenAzModal("编辑模具信息Edit mold information", "/MoldUseTime/Page/MoldDetail?MoldID="+datas.ID, function () {
       ListMoldUseTimeForPage(vue);
    });
    ResizeAzModal(1000);
}


/**
 * 导出设备信息
 */
function ExportMoldDetail(vue) {
    vue.isExportLoading = true;
    vue.isExportDisabled = true;
    $.ajax({
        type: "POST",
        url: "/MoldUseTime/ExportMoldData?v=" + Math.random(),
        data: {
            searchModel: vue.searchModel
        },
        success: function (res) {
            if (res.status == "Success") {
                window.location.href = res.data;
            } else {
                toastr.error(res.message);
            }
        },
        error: function (err) {
            console.log(err);
            toastr.error("加载数据异常，请稍后再试或联系管理员Loading data is abnormal. Please try again later or contact the administrator。");
        },
        complete: function () {
            HideAzLoading();
            vue.isExportLoading = false;
            vue.isExportDisabled = false;
        }
    });
}

/**
 * 删除设备信息
 */
function DeleteMoldDetail(vue, mold) {
    var message = "确认删除模具序号为Confirm to delete the mold serial number as：【" + mold + "】?";
    if (confirm(message)) {
        ShowAzLoading();
        $.ajax({
            type: "POST",
            url: "/MoldUseTime/DeleteMold",
            data: { MoldID: mold },
            success: function (res) {
                if (res.status == "Success") {
                    toastr.success("删除模具信息成功Mold information deleted successfully。");
                    ListMoldUseTimeForPage(vue);
                } else {
                    toastr.error(res.message);
                }
                HideAzLoading();
            },
            error: function (err) {
                console.log(err);
                toastr.error("服务器发生错误，请稍候再试An error occurred in the server. Please try again later。");
                HideAzLoading();
            }
        });
    }
}

function ResetPageModel(vue) {
    vue.pageModel.pageIdx = 1;
    vue.pageModel.pageSize = 10;
    vue.pageModel.pageLength = 0;
    vue.pageModel.pageTotal = 0;

    vue.searchModel.queryMoldID = "";
    vue.searchModel.querySegment = "";
    vue.searchModel.orderByMoldID = false;
    vue.searchModel.orderBySegment = false;
    vue.searchModel.isAsc = true;
    vue.searchModel.isAll = true;
}

